﻿using System;
using System.Reflection;
using System.Configuration;

namespace RBAC.CacheFactory
{
    internal sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        public static readonly bool EnableCache = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["EnableCache"]);

        public DataAccess()
        { }

        #region CreateObject

        //不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch//(System.Exception ex)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }
        //使用缓存
        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                }
            }
            return objType;
        }
        #endregion

        #region
        /// <summary>
        /// 创建IAccess数据层接口
        /// </summary>
        public static IDAL.IAccess CreateAccess()
        {
            string ClassNamespace = AssemblyPath + ".Access";
            object objType;
            if (EnableCache)
                objType = CreateObject(AssemblyPath,ClassNamespace);
            else
                objType = CreateObjectNoCache(AssemblyPath, ClassNamespace);
            return (IDAL.IAccess)objType;
        }
        #endregion

        #region
        /// <summary>
        /// 创建INode数据层接口
        /// </summary>
        public static IDAL.INode CreateNode()
        {
            string ClassNamespace = AssemblyPath + ".Node";
            object objType;
            if (EnableCache)
                objType = CreateObject(AssemblyPath, ClassNamespace);
            else
                objType = CreateObjectNoCache(AssemblyPath, ClassNamespace);
            return (IDAL.INode)objType;
        }
        #endregion

        #region
        /// <summary>
        /// 创建INodeGroup数据层接口
        /// </summary>
        public static IDAL.INodeGroup CreateNodeGroup()
        {
            string ClassNamespace = AssemblyPath + ".NodeGroup";
            object objType;
            if (EnableCache)
                objType = CreateObject(AssemblyPath, ClassNamespace);
            else
                objType = CreateObjectNoCache(AssemblyPath, ClassNamespace);
            return (IDAL.INodeGroup)objType;
        }
        #endregion

        #region
        /// <summary>
        /// 创建IRole数据层接口
        /// </summary>
        public static IDAL.IRole CreateRole()
        {
            string ClassNamespace = AssemblyPath + ".Role";
            object objType;
            if (EnableCache)
                objType = CreateObject(AssemblyPath, ClassNamespace);
            else
                objType = CreateObjectNoCache(AssemblyPath, ClassNamespace);
            return (IDAL.IRole)objType;
        }
        #endregion

        #region
        /// <summary>
        /// 创建IUserRoleID数据层接口
        /// </summary>
        public static IDAL.IUserRoleID CreateUserRoleID()
        {
            string ClassNamespace = AssemblyPath + ".UserRoleID";
            object objType;
            if (EnableCache)
                objType = CreateObject(AssemblyPath, ClassNamespace);
            else
                objType = CreateObjectNoCache(AssemblyPath, ClassNamespace);
            return (IDAL.IUserRoleID)objType;
        }
        #endregion
    }
}
